package com.menggudf.controller;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.menggudf.model.ResultModel;
import com.menggudf.model.ResultStatus;
import com.menggudf.model.UserModel;
import com.menggudf.service.UserService;

/**
 * 手机端用户controller
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/user")
public class UserController {
	
	@Autowired
	UserService userService;
	
	
	@RequestMapping("/login")
	@ResponseBody
	public ResultModel  login(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam("phone") String phone,
			@RequestParam("psw") String passWord,  
			HttpSession httpSession){
		 ResultModel model =userService.userLogin(phone, passWord);
		 if(model.getResultCode()==ResultStatus.RESULT_STATUS_SUCCESS.getValue()){
	            UserModel userModel = (UserModel) model.getContent();
	            httpSession.setAttribute("userModel", userModel);
	        }
		 return model;
		 
	}
	
	/**
	 * 退出登录
	 * @param request
	 * @param response
	 * @param httpSession
	 * @return
	 */
	@RequestMapping("/loginOut")
	@ResponseBody
	public ResultModel loginOut(HttpServletRequest request,
			HttpServletResponse response,HttpSession httpSession){
		if(httpSession.getAttribute("userModel")!=null){
			httpSession.removeAttribute("userModel");
		}
		if(httpSession.getAttribute("userModel")==null){
			 return ResultModel.success();
		}else{
			 return ResultModel.fail();
		}
	}
	
	   /**
     * 获取用户列表
     * @return
     */
    @RequestMapping("/getUserList")
    @ResponseBody
    public Map<String, Object> getUserList(
            @RequestParam(value="userCode",required=false) String userCode,
            @RequestParam(value="userName",required=false) String userName,
            @RequestParam("limit") int pageSize,
            @RequestParam("offset") int offset,
            @RequestParam("startTime") Date startTime,
            @RequestParam("endTime") Date endTime){
                return userService.getUserList(userCode,userName,pageSize,offset,startTime,endTime);
        
    }
    
    @RequestMapping("/getUserDetail")
    @ResponseBody
    public UserModel getUserDetail(
            @RequestParam(value="userCode",required=false) String userCode){
                return userService.getUserDetail(userCode);
        
    }
}
